package com.learn.hibernatevalidator.chapter02.containerelement.map;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;

import java.lang.annotation.*;

/**
 * @author: liyf
 * @create: 2024-12-20 22:46
 **/
@Documented
@Constraint(validatedBy = {MaxAllowedFuelConsumption.MaxAllowedFuelConsumptionValidator.class})
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MaxAllowedFuelConsumption {

    String message() default "value 0~10 之间";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class MaxAllowedFuelConsumptionValidator implements ConstraintValidator<MaxAllowedFuelConsumption, Integer> {

        @Override
        public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
            if (value == 0) {
                return true;
            }
            return value >= 0 && value <= 10;
        }
    }
}
